package com.szsky.sky.demo;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 数字社科门户-OA办公管理系统，账号/密码加解密，登录admin的加解密。
 * http://szsk.sky/vportal-api/j_spring_security_check
 * /user/updatePassword
 */
public final class AESPtUtils_TYYH {
    private static final Logger logger = LoggerFactory.getLogger(AESPtUtils_TYYH.class);


    private static String KEY = "d5e3wmyuyt4riyq2";

    private static String IV = "fiyme84mairygdwl";


    public static String encrypt(String data, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength += blockSize - plaintextLength % blockSize;
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(1, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return (new Base64()).encodeToString(encrypted);
        } catch (Exception e) {
            logger.error("", e);
            return null;
        }
    }


    public static String decrypt(String data, String key, String iv) {
        try {

            byte[] encrypted1 = (new Base64()).decode(data);


            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());


            cipher.init(2, keyspec, ivspec);


            byte[] original = cipher.doFinal(encrypted1);

            return new String(original);

        } catch (Exception e) {

            logger.error("解密失败", e);

            return null;
        }
    }


    public static String encrypt(String data) {

        return encrypt(data, KEY, IV);
    }


    public static String decrypt(String data) {

        return StringUtils.trim(decrypt(data, KEY, IV));
    }

    public static void main(String[] args) {
        System.out.println(StringUtils.trim(decrypt("Fuq5sZCQaz1ZtEKAiXlLwg==", KEY, IV)));
        System.out.println(StringUtils.trim(decrypt("54ep5ZCj5YvY9sY/9xOd5Q==", KEY, IV)));
        System.out.println(StringUtils.trim(decrypt("CtrCDm/8JfbBJm7vwOqm0Q==", KEY, IV)));
                /*
        d145fd2a25d05c21bedf2beffccf9d6db242dbf8c893acdae7f21b3cb4b795e7
        92434f3fd1f694baa5f52449a52cbdae04dbfc388a47fd8d4b8528283cfaadaf
        94be5921c87b1c07fd04b0e57ec5fe79c2c2c53e255bf832de2cb770c73ebf00
        bddd880bd3cd5264d5a8ace2c9ee0581c50968b947fea44b26b572cd077fbd98
        */
        System.out.println(StringUtils.trim(decrypt("RoBafNNRDf4rXg5+fv0QiA==", KEY, IV)));
        System.out.println(StringUtils.trim(decrypt("tps/8EYBWufhioNum3kTCg==", KEY, IV)));
    }
}
